package com.taptech.doufu.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.TextUtil;

/**
 * 用户等级信息
 */
public class UserGradeView extends LinearLayout {
    private float nameSize;
    private int nameColor;
    private TextView tvUserName;
    private TextView tvAuthorFlag;
    private ImageView ivLevel;
    private ImageView ivVip;
    private View view;
    private Context mContext;

    public UserGradeView(Context context) {
        super(context);
        mContext = context;
    }

    public UserGradeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        view = LayoutInflater.from(context).inflate(R.layout.view_user_grade, null);
        tvUserName = view.findViewById(R.id.tvUserName);
        tvAuthorFlag = view.findViewById(R.id.tvAuthorFlag);
        ivLevel = view.findViewById(R.id.ivLevel);
        ivVip = view.findViewById(R.id.ivVip);
        initAttr(context,attrs);
        addView(view);
    }

    public UserGradeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    private void initAttr(Context context,AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.UserGradeView);
        nameSize = ta.getDimensionPixelSize(R.styleable.UserGradeView_nameSize, 0);
        nameColor = ta.getColor(R.styleable.UserGradeView_nameColor, 0);
        String fontFamily = ta.getString(R.styleable.UserGradeView_nameFontFamily);
        if (!TextUtils.isEmpty(fontFamily)) {
            Typeface font = Typeface.create(fontFamily, Typeface.NORMAL);
            tvUserName.setTypeface(font);
        }
    }


    public void setData(final UserBean userBean, boolean showAuthor) {
        if (userBean != null) {
            if (nameSize > 0) {
                tvUserName.setTextSize(ScreenUtil.px2dip(mContext,nameSize));
            }
            if (nameColor != 0) {
                tvUserName.setTextColor(nameColor);
            }
            if (!TextUtil.isEmpty(userBean.getNickname())) {
                tvUserName.setText(userBean.getNickname());
            }else{
                tvUserName.setText(userBean.getName());
            }
            GlideUtil.displayImage(ivLevel,userBean.getLevel_icon());
            ivVip.setVisibility(VISIBLE);
            if (userBean.getIs_year_vip() > 0) {
                ivVip.setImageResource(R.drawable.icon_year_vip);
                tvUserName.setTextColor(Color.parseColor("#fe6e6e"));
            } else if (userBean.getIs_vip() > 0) {
                ivVip.setImageResource(R.drawable.icon_normal_vip);
                tvUserName.setTextColor(Color.parseColor("#fe6e6e"));
            }else{
                ivVip.setVisibility(GONE);
            }
        }
        if (showAuthor) {
            tvAuthorFlag.setVisibility(VISIBLE);
        }else{
            tvAuthorFlag.setVisibility(GONE);
        }
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String uid = userBean.getUid();
                if (TextUtil.isEmpty(uid)) {
                    uid = userBean.getUserId();
                }
                UserDetailActivity.Companion.startActivity(mContext,uid);
            }
        });
    }

}
